package mulMan.gui.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Observable;

import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JFileChooser;

import mulMan.domain.AudioFileModel;
import mulMan.gui.view.AddFileView;
import mulMan.gui.view.AudioFileView;
import mulMan.gui.view.ModifyFileView;

public class AudioFileController extends Observable{
	
	private AudioFileModel audioFile;
	private AudioFileView view;
	
	
	public AudioFileModel getAudioFile() {
		return audioFile;
	}
	public void setAudioFile(AudioFileModel audioFile) {
		this.audioFile = audioFile;
	}

	public AudioFileView getView() {
		return view;
	}
	public void setView(AudioFileView view) {
		this.view = view;
	}
	
	
	public AudioFileController(AudioFileModel audioFile, AudioFileView view) {
		this.audioFile = audioFile;
		this.view = view;
		
		view.addFindAudioFileListener(new FindAudioFileListener());
		
		if (view instanceof ModifyFileView) {
			((ModifyFileView)view).addModifyAudioFileListener(new ModifyAudioFileListener());
			((ModifyFileView)view).addPlayAudioFileListener(new PlayAudioFileListener());
		}
		
		if (view instanceof AddFileView) {
			((AddFileView)view).addAddAudioFileListener(new AddAudioFileListener());
		}
	}
	
	
	//ACTION LISTENERS
	
	class ModifyAudioFileListener implements ActionListener {
	    public void actionPerformed(ActionEvent e) {
	    	audioFile.getAudioFile().setTitle(view.getTitleTextField().getText());
	    	audioFile.getAudioFile().setAlbum(view.getAlbumTextField().getText());
	    	audioFile.getAudioFile().setArtist(view.getArtistTextField().getText());
	    	audioFile.getAudioFile().setLength(Integer.valueOf(view.getLengthTextField().getText()));
	    	audioFile.getAudioFile().setPath(view.getPathTextField().getText());
	    	try {
	    		System.out.println("ID: " + audioFile.getAudioFile().getIdAudioFile());
				audioFile.updateAudioFile();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
	    	((ModifyFileView)view).dispose();
	    }
	}
	
	class PlayAudioFileListener implements ActionListener {
	    public void actionPerformed(ActionEvent e) {
	    	
			try {
				audioFile.play();
			} catch (UnsupportedAudioFileException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			} catch (LineUnavailableException e1) {
				e1.printStackTrace();
			}
	    }
	}
	
	class AddAudioFileListener implements ActionListener {
	    public void actionPerformed(ActionEvent e) {
	    	audioFile.getAudioFile().setTitle(view.getTitleTextField().getText());
	    	audioFile.getAudioFile().setAlbum(view.getAlbumTextField().getText());
	    	audioFile.getAudioFile().setArtist(view.getArtistTextField().getText());
	    	audioFile.getAudioFile().setLength(Integer.valueOf(view.getLengthTextField().getText()));
	    	audioFile.getAudioFile().setPath(view.getPathTextField().getText());
	    	try {
				audioFile.saveAudioFile();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
	    	((AddFileView)view).getButtonAdd().setEnabled(false);
	    	((AddFileView)view).dispose();
	    }
	}
	
	class FindAudioFileListener implements ActionListener {
	    public void actionPerformed(ActionEvent e) {
	    	int returnVal = view.getChooser().showOpenDialog(null);
		    if(returnVal == JFileChooser.APPROVE_OPTION) {
		       view.getPathTextField().setText(view.getChooser().getSelectedFile().getAbsolutePath());
		    }
	    }
	}
}
